/*
6_RobustnessLeaveOneOut.do

Makes sensitivity figures for the appendix. Output of this file is read into 
	07_LeaveOneOutFigures.R for plotting


"Human Capital and Climate Change", 
	by Noam Angrist, Kevin Winseck, Harry Patrinos, and Joshua Graff Zivin.
		
Inputs:
	Cleaned_ESS_withFirstStage.dta
Outputs:
	RobustnessLeaveOneOut_Reform_beliefs.tex
	RobustnessLeaveOneOut_Reform_behaviors.tex
	RobustnessLeaveOneOut_Reform_policy.tex
	RobustnessLeaveOneOut_Reform_voting.tex
	
	RobustnessLeaveOneOut_Reform_beliefs.csv
	RobustnessLeaveOneOut_Reform_behaviors.csv
	RobustnessLeaveOneOut_Reform_policy.csv
	RobustnessLeaveOneOut_Reform_voting.csv	
	
	RobustnessLeaveOneOut_Country_beliefs.tex
	RobustnessLeaveOneOut_Country_behaviors.tex
	RobustnessLeaveOneOut_Country_policy.tex
	RobustnessLeaveOneOut_Country_voting.tex
	
	RobustnessLeaveOneOut_Country_beliefs.csv
	RobustnessLeaveOneOut_Country_behaviors.csv
	RobustnessLeaveOneOut_Country_policy.csv
	RobustnessLeaveOneOut_Country_voting.csv
		
	BalanceTest_AnyReform.tex (Appendix Table A6)

*/
global clustervar "cntryXlaw"

use "Cleaned_ESS_withFirstStage.dta", clear

drop if mi(minEdu)
* Drop if missing CSL data




eststo clear





******* Estimates when we leave one reform out *********************************


*local var "beliefs"
*local var "behaviors"
*local var "policy"
*local var "voting"
foreach var in beliefs behaviors policy voting{

	levelsof cntryXlaw if in_sample==1 & in_pooled==1 , local(reforms)
	foreach reform in `reforms'{

		eststo: ivregress 2sls `var'_proenv (eduyrs_wins=reform_pos_sig_* reform_pos_only_* reform_only_*) yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1   & cntryXlaw!="`reform'"   /*, vce(cluster $clustervar)*/ /*standard errors don't matter because we only care about point estimates in this exercise*/

	}
	esttab using "RobustnessLeaveOneOut_Reform_`var'.tex", replace cells("b(fmt(5))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) /*scalars(Clusters Mean PercentChange)*/ keep(eduyrs_wins) label nostar nonote
	esttab using "RobustnessLeaveOneOut_Reform_`var'.csv", replace cells("b(fmt(5))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) /*scalars(Clusters Mean PercentChange)*/ keep(eduyrs_wins) label nostar nonote

	******* Estimates when we leave one reform out *********************************

	eststo clear
	levelsof cntry if in_sample==1 & in_pooled==1 , local(countries)

	foreach country in `countries'{
		eststo: ivregress 2sls `var'_proenv (eduyrs_wins=reform_pos_sig_* reform_pos_only_* reform_only_*) yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1 & cntry!="`country'"   /*, vce(cluster $clustervar)*/
		
	}

	esttab using "RobustnessLeaveOneOut_Country_`var'.tex", replace cells("b(fmt(5))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) /*scalars(Clusters Mean PercentChange)*/ keep(eduyrs_wins) label nostar nonote
	esttab using "RobustnessLeaveOneOut_Country_`var'.csv", replace cells("b(fmt(5))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) /*scalars(Clusters Mean PercentChange)*/ keep(eduyrs_wins) label nostar nonote

}



********************************************************************************
* Balance tests
* Appendix Table A6
********************************************************************************



* Clean the variables we want to check balance on
gen male = 1 if gndr==1
replace male = 0 if gndr==2

gen brncntr_recoded = 1 if brncntr==1
replace brncntr_recoded = 0 if brncntr==2

gen any_reform = .
foreach var of varlist reform_pos_sig_* reform_pos_only_* reform_only_*{
	replace any_reform = `var' if any_reform != 1
}
tab any_reform

eststo clear

eststo: reg male any_reform yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1 &!mi(beliefs_proenv) , vce(cluster $clustervar)
eststo: reg brncntr_recoded any_reform yrbrn b2.cntry_numeric if in_sample==1 & in_pooled==1 &!mi(beliefs_proenv), vce(cluster $clustervar)
*eststo: reg eduyrs_wins any_reform yrbrn b2.cntry_numeric if in_sample==1 & in_pooled==1 &!mi(beliefs_proenv), vce(cluster $clustervar)
esttab

esttab using "BalanceTest_AnyReform.tex", replace cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) /*scalars(Clusters Mean PercentChange FourYearEffect TimeTrend CountryFE ClusterVar)*/ keep(any_reform) label star(* 0.10 ** 0.05 *** 0.01) note(Standard errors in parentheses. P-values in brackets.) mtitles("Male" "Born in Country" /*"Years of Education"*/)
